﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Manager.Camada.Logica;
using Manager.Camada.Negocio;
using System.Data.SqlClient;
using System.Data;

namespace Manager.Camada.BancoDados
{
    public class PermissaoDAOImpl : PermissaoDAO
    {
        private static string strConnection = "Data Source=ICARO-PC;Initial Catalog=Gerenciador;Integrated Security=True";

        public PermissaoVO getPermissaoVO(int idPermissao)
        {
            SqlConnection sqlconnection = new SqlConnection(strConnection);

            PermissaoVO permissao = new PermissaoVO();

            try
            {
                string sqlBusca = "SELECT * FROM PERMISSAO WHERE idPermissao = @IdPermissao";

                sqlconnection.Open();

                SqlCommand cmd = new SqlCommand(sqlBusca, sqlconnection);

                cmd.Parameters.AddWithValue("@IdPermissao", idPermissao);

                SqlDataReader dr = cmd.ExecuteReader();

                DataTable tblPermissao = new DataTable();
                tblPermissao.Load(dr);

                if (tblPermissao.Rows.Count > 0)
                {
                    permissao = mountPermissao(tblPermissao);
                }

            }
            catch (Exception exSql)
            {
                return null;
            }

            return permissao;
        }

        private PermissaoVO mountPermissao(DataTable tblPermissao)
        {
            PermissaoVO permissao = new PermissaoVO();

            foreach (DataRow row in tblPermissao.Rows)
            {
                permissao.idPermissao = Convert.ToInt32(row["idPermissao"]);
                permissao.permissao = Convert.ToString(row["permissao"]);
            }

            return permissao;
        }
    }
}
